# -*- coding: UTF-8 -*- #
"""
-------真正的勇士， 敢于直面惨淡的Warning、 敢于正视淋漓的Error-----
@author : 贾鹏博
@file : 12、接口自动化测试的form请求.py
@time : 2022/5/15 12:20
"""

"""
什么是 form 请求
    数据量不大
    数据层级不深的情况
    通常以键值对传递
    "Content-Type": "application/x-www-form-urlencoded"
"""

import requests


class TestReq:

    def setup_class(self):
        # 设置代理
        self.proxy = {"http": "http://127.0.0.1:8080", "https": "http://127.0.0.1:8080"}

    # ====================================== form 请求： 使用 data=data ====================================================
    def test_data(self):
        data = {"hogwarts": "school"}
        # 通过 data 参数传入请求体信息
        r = requests.post("https://httpbin.testing-studio.com/post", data=data, proxies=self.proxy, verify=False)
        # 返回结果 ： {'args': {}, 'data': '', 'files': {}, 'form': {'hogwarts': 'school'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '15', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.testing-studio.com', 'User-Agent': 'python-requests/2.27.1', 'X-Forwarded-Host': 'httpbin.testing-studio.com', 'X-Forwarded-Scheme': 'https', 'X-Scheme': 'https'}, 'json': None, 'origin': '172.17.56.110', 'url': 'https://httpbin.testing-studio.com/post'}
        print(r.json())
        assert r.json()['form']['hogwarts'] == 'school'

    # ==================================  json 请求：使用 json=data =========================================================
    # json格式的      "Content-Type": "application/json“
    def test_json(self):
        data = {"hogwarts": "school"}
        # 通过 json 参数传入请求体信息
        r = requests.post("https://httpbin.testing-studio.com/post", json=data, proxies=self.proxy, verify=False)
        # 返回结果 ： {'args': {}, 'data': '{"hogwarts": "school"}', 'files': {}, 'form': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '22', 'Content-Type': 'application/json', 'Host': 'httpbin.testing-studio.com', 'User-Agent': 'python-requests/2.27.1', 'X-Forwarded-Host': 'httpbin.testing-studio.com', 'X-Forwarded-Scheme': 'https', 'X-Scheme': 'https'}, 'json': {'hogwarts': 'school'}, 'origin': '10.7.136.128', 'url': 'https://httpbin.testing-studio.com/post'}
        print(r.json())
        assert r.json()['json']['hogwarts'] == 'school'
